package com.sam.lazyload;

public class Instance {

	private Instance() {

	}

	private static Instance instante = null;

	/**
	 * 非线程安全 单例延迟初 始化实现
	 * 
	 * @return
	 */
	public static Instance getInstance() {

		if (instante == null) {
			instante = new Instance();
		}
		return instante;
	}

	/**
	 * 保证多线程环境下 只能创建一个实例 但是加锁对性能 影响较大
	 * 
	 * @return
	 */
	public synchronized static Instance getInstance2() {
		if (instante == null) {
			instante = new Instance();
		}
		return instante;
	}

	public static void main(String[] args) {

		for (int i = 0; i < 100; i++) {

			new Thread(new Runnable() {

				@Override
				public void run() {

					System.out.println(Thread.currentThread().getName()
							+ "获取实例：" + Instance.getInstance2().toString());

				}
			}).start();
		}

		System.out.println(Thread.currentThread().getName() + "获取实例："
				+ Instance.getInstance2().toString());

	}

}
